package commons;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.faces.event.ValueChangeEvent;

/**
 * Manager konfiguracyjny komponent wyboru daty ( <rich:date > )
 * 
 * @author Michał Budziło
 * 
 */
public class CalendarManager {

	/**
	 * Polskie skróty dni tygodnia
	 */
	private static final String[] WEEK_DAY_LABELS = new String[] { "N *",
			"P +", "W +", "Ś +", "C +", "P +", "S *" };
	private Locale locale;

	/**
	 * Czy okienko wyboru daty ma się pojawiać, czy być stałe.
	 */
	private boolean popup;
	/**
	 * Czy data jest tylko do odczytu
	 */
	private boolean readonly;
	/**
	 * Czy pokazywać wybraną datę
	 */
	private boolean showInput;
	/**
	 * Czy można wpisywać datę ręcznie
	 */
	private boolean enableManualInput;
	/**
	 * Wzór wyświetlanej daty
	 */
	private String pattern;
	/**
	 * Aktualnie zaznaczona data
	 */
	private Date currentDate;
	/**
	 * Wybrana data
	 */
	private Date selectedDate;

	/**
	 * Kierunek wysuwania komponentu
	 */
	private String jointPoint;
	/**
	 * Kierunek
	 */
	private String direction;
	/**
	 * Granica
	 */
	private String boundary;

	/**
	 * Wybr
	 */
	private boolean useCustomDayLabels;

	/**
//	 * Konstruktor, ustawia domyslne parametry
	 */
	public CalendarManager() {

		locale = new Locale("pl", "PL");
		popup = true;
		pattern = "yyyy-MM-dd";
		jointPoint = "bottomleft";
		direction = "bottomright";
		readonly = true;
		enableManualInput = false;
		showInput = true;
		boundary = "inactive";
	}

	/**
	 * Metoda przeksztalca obiekt calendar na date w postaci String
	 * @param cal obiekt kalendarza
	 * @return data w postaci String
	 */
	public String CalendarToString(Calendar cal) {
		String t;
		String month;
		String year;
		String day;

		year = "" + cal.get(Calendar.YEAR);

		if ((cal.get(Calendar.MONTH) + 1) <= 9) {
			month = "0" + (cal.get(Calendar.MONTH) + 1);
		} else {
			month = "" + (cal.get(Calendar.MONTH) + 1);
		}

		if (cal.get(Calendar.DAY_OF_MONTH) <= 9) {
			day = "0" + cal.get(Calendar.DAY_OF_MONTH);
		} else {
			day = "" + cal.get(Calendar.DAY_OF_MONTH);
		}

		t = year + "-" + month + "-" + day;
		return t;

	}

	/**
	 * Metoda przekształca datę w postaci ciągu znaków na datę obiektu sql.Date
	 * @param t data w postaci ciągu znaków
	 * @return obiekt klasy sql.Date
	 */
	public java.sql.Date StringToDate(String t) {
		java.sql.Date a = null;
		SimpleDateFormat ts = new SimpleDateFormat("yyyy-MM-dd");
		try {
			a = new java.sql.Date(ts.parse(t).getTime());
			return a;
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

		}
		return a;
	}

	/**
	 * Metoda generuje listę dat z podanego przedziału 
	 * (potrzebna przy generowaniu dziedziny w statystykach)
	 * @param start data początkowa
	 * @param end data końcowa
	 * @return lista typu String
	 */
	public List<String> generateDateList(String start, String end) {
		List<String> listaDat = new ArrayList<String>();
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		String t = "";
		try {
			cal.setTime(dateFormat.parse(start));
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		while (!t.equals(end)) {

			t = CalendarToString(cal);
			listaDat.add(t);
			cal.add(Calendar.DATE, 1);
		}
		return listaDat;

	}

	
	public void selectJointPoint(ValueChangeEvent event) {
		jointPoint = (String) event.getNewValue();
	}

	

	public void selectDirection(ValueChangeEvent event) {
		direction = (String) event.getNewValue();
	}

	/**
	 * @return the locale
	 */
	public Locale getLocale() {
		return locale;
	}

	/**
	 * @param locale the locale to set
	 */
	public void setLocale(Locale locale) {
		this.locale = locale;
	}

	/**
	 * @return the popup
	 */
	public boolean isPopup() {
		return popup;
	}

	/**
	 * @param popup the popup to set
	 */
	public void setPopup(boolean popup) {
		this.popup = popup;
	}

	/**
	 * @return the readonly
	 */
	public boolean isReadonly() {
		return readonly;
	}

	/**
	 * @param readonly the readonly to set
	 */
	public void setReadonly(boolean readonly) {
		this.readonly = readonly;
	}

	/**
	 * @return the showInput
	 */
	public boolean isShowInput() {
		return showInput;
	}

	/**
	 * @param showInput the showInput to set
	 */
	public void setShowInput(boolean showInput) {
		this.showInput = showInput;
	}

	/**
	 * @return the enableManualInput
	 */
	public boolean isEnableManualInput() {
		return enableManualInput;
	}

	/**
	 * @param enableManualInput the enableManualInput to set
	 */
	public void setEnableManualInput(boolean enableManualInput) {
		this.enableManualInput = enableManualInput;
	}

	/**
	 * @return the pattern
	 */
	public String getPattern() {
		return pattern;
	}

	/**
	 * @param pattern the pattern to set
	 */
	public void setPattern(String pattern) {
		this.pattern = pattern;
	}

	/**
	 * @return the currentDate
	 */
	public Date getCurrentDate() {
		return currentDate;
	}

	/**
	 * @param currentDate the currentDate to set
	 */
	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	/**
	 * @return the selectedDate
	 */
	public Date getSelectedDate() {
		return selectedDate;
	}

	/**
	 * @param selectedDate the selectedDate to set
	 */
	public void setSelectedDate(Date selectedDate) {
		this.selectedDate = selectedDate;
	}

	/**
	 * @return the jointPoint
	 */
	public String getJointPoint() {
		return jointPoint;
	}

	/**
	 * @param jointPoint the jointPoint to set
	 */
	public void setJointPoint(String jointPoint) {
		this.jointPoint = jointPoint;
	}

	/**
	 * @return the direction
	 */
	public String getDirection() {
		return direction;
	}

	/**
	 * @param direction the direction to set
	 */
	public void setDirection(String direction) {
		this.direction = direction;
	}

	/**
	 * @return the boundary
	 */
	public String getBoundary() {
		return boundary;
	}

	/**
	 * @param boundary the boundary to set
	 */
	public void setBoundary(String boundary) {
		this.boundary = boundary;
	}

	/**
	 * @return the useCustomDayLabels
	 */
	public boolean isUseCustomDayLabels() {
		return useCustomDayLabels;
	}

	/**
	 * @param useCustomDayLabels the useCustomDayLabels to set
	 */
	public void setUseCustomDayLabels(boolean useCustomDayLabels) {
		this.useCustomDayLabels = useCustomDayLabels;
	}

	/**
	 * @return the weekDayLabels
	 */
	public static String[] getWeekDayLabels() {
		return WEEK_DAY_LABELS;
	}

	

}
